#include "DHT11.h"

/*宏定义*/
#define DHT11_IO 25

/*全局变量*/
char temp[10] = {"\0"};
char humi[10] = {"\0"};
extern volatile uint8_t atomizer_switch;//雾化器开关标志 0--关闭 1--开启
extern volatile uint8_t manual_open_atomizer_flag;//雾化器手动开启标志 0--非手动 1--手动 2--自动
extern uint8_t month;//月

/*局部函数*/
static void IO_Output(void)
{
    pinMode(DHT11_IO, OUTPUT);
}

static void IO_Input(void)
{
    pinMode(DHT11_IO, INPUT_PULLUP);
}

static void DHT11_Rst(void)
{
    IO_Output();//IO口设置为输出
    digitalWrite(DHT11_IO, LOW);//拉低总线
    delay(20);//拉低至少18ms

    digitalWrite(DHT11_IO, HIGH);//拉高总线
    delayMicroseconds(30);//主机拉高20~40us
}

static uint8_t DHT11_Check(void)
{
    uint8_t retry = 0;

    IO_Input();//IO口设置为输入

    //总线拉低40~80us，等待总线拉高
    while((digitalRead(DHT11_IO) == HIGH) && (retry < 100))
    {
        retry++;
        delayMicroseconds(1);
    }

    if(retry >= 100)
    {
        return 1;
    }
    else
    {
        retry = 0;
    }

    //总线拉高40~80us，等待总线变高
    while((digitalRead(DHT11_IO) == LOW) && (retry < 100))
    {
        retry++;
        delayMicroseconds(1);
    }

    if(retry >= 100)
    {
        return 1;
    }

    return 0;
}

//从DHT11读取一个位
static uint8_t DHT11_Read_Bit(void)
{
    uint8_t retry = 0;

    //等待变低电平
    while((digitalRead(DHT11_IO) == HIGH) && (retry < 100))
    {
        retry++;
        delayMicroseconds(1);
    }

    retry = 0;
    while((digitalRead(DHT11_IO) == LOW) && (retry < 100))//等待变高电平
    {
        retry++;
        delayMicroseconds(1);
    }

    delayMicroseconds(40);//等待40us

    if(digitalRead(DHT11_IO) == HIGH)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

static uint8_t DHT11_Read_Byte(void)
{
    uint8_t dat;
    dat = 0;
    for(uint8_t i = 0; i < 8; i++)
    {
        dat <<= 1;
        dat |= DHT11_Read_Bit();
    }

    return dat;
}

/*全局函数*/
uint8_t DHT11_Read_Data(void)
{
    static uint8_t idx = 0;
    uint8_t buf[5];

    DHT11_Rst();
    if(DHT11_Check() == 0)
    {
        for(uint8_t i = 0; i < 5; i++)
        {
            buf[i] = DHT11_Read_Byte();
        }

        if((buf[0] + buf[1] + buf[2] + buf[3]) == buf[4])
        {
            // Serial.println(buf[0]);
            // Serial.println(buf[2]);
            //Serial.printf("humi:%d,temp:%d\r\n",buf[0],buf[2]);
            sprintf(temp,"%d.%d",buf[2],buf[3]);
            sprintf(humi,"%d.%d",buf[0],buf[1]);

            idx++;

            if(idx >= 5)
            {
                Update_Temp_Humi_To_Aliyun(temp, humi);
                idx = 0;
                float f_humi = atof(humi);
                if(manual_open_atomizer_flag == 1)//自动模式开启
                {
                    if(f_humi <= 40.0 && (month >= 3  && month <= 9) )
                    {
                        //Atomizer_Open();
                        //atomizer_switch = 1;
                        if(atomizer_switch == 0)
                        {
                            Atomizer_Open();
                            atomizer_switch = 1;
                            WS2812B_SetGRB(120, 10, 0);//警戒颜色 红橙色
                        }
                    }
                    else if(f_humi > 40.0 && (month >= 3 && month <= 9) )
                    {
                        //Atomizer_Close();
                        //atomizer_switch = 0;
                        if(atomizer_switch == 1)
                        {
                            Atomizer_Close();
                            atomizer_switch = 0;
                            WS2812B_Close();//关闭WS2812B灯带

                        }
                    }

                    if(f_humi <= 30.0 && ( (month >= 10 && month <= 12) || (month >= 1 && month <= 2) ) )
                    {
                        if(atomizer_switch == 0)
                        {
                            Atomizer_Open();
                            atomizer_switch = 1;
                            WS2812B_SetGRB(120, 10, 0);//警戒颜色 红橙色
                        }
                    }
                    else if(f_humi > 30.0 &&  ( (month >= 10 && month <= 12) || (month >= 1 && month <= 2) ) )
                    {
                        if(atomizer_switch == 0)
                        {
                            Atomizer_Close();
                            atomizer_switch = 0;
                            WS2812B_Close();//关闭WS2812B灯带
                        }
                    }
                    Update_Humidified_To_Aliyun(atomizer_switch);
                }
                else//手动模式
                {
                    
                }
            }
            //Serial.printf("%.1f\r\n",atof(humi));
            // if(humi)
            //Serial.printf("humi:%s,temp:%s\r\n",humi,temp);
        }
    }
    else
    {
        return 1;
    }

    return 0;
}

uint8_t DHT11_Init(void)
{
    pinMode(DHT11_IO, INPUT_PULLUP);//上拉输入

    DHT11_Rst();//DHT11复位

    return DHT11_Check();
}

